package com.lbd.system.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Employee;
import com.lbd.system.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * Created by WangYajie on 2016/9/12.
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    EmployeeService employeeService;

    /**
     * 根据查询条件分页查询员工
     *
     * @param page     第几页
     * @param rows     一页多少条
     * @param employee 员工信息
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Employee employee) {
        return new Result(ResultState.SUCCESS, "查询成功", employeeService.findByPageConditionJpql(page, rows, employee));
    }

    /**
     * 保存员工信息
     *
     * @param employee
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Employee employee) {
        employeeService.save(employee);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 删除员工信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        employeeService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 根据ID查询要修改的员工信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", employeeService.findById(id));
    }

    /**
     * 修改员工信息
     *
     * @param employee
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Employee employee) {
        employeeService.update(employee);
        return new Result(ResultState.SUCCESS, "更新成功");
    }


    /**
     * 员工登录
     *
     * @param employee
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(Employee employee, String varCode, HttpSession session) {
        String sessionVarCode = (String) session.getAttribute("loginVar");
        if (StringUtils.isNotBlank(varCode) && !varCode.equals(sessionVarCode)) {
            session.setAttribute("loginVar", "");
            return new Result(ResultState.FAIL, "验证码错误");
        }
        try {
            Employee emp = employeeService.login(employee);
            session.setAttribute(Employee.SESSION_USER, emp);
        } catch (Exception e) {
            log.info("用户名或者密码错误" + e);
            return new Result(ResultState.FAIL, "用户名或者密码错误");
        }
        return new Result(ResultState.SUCCESS, "登录成功");
    }

    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void login(HttpSession session) {
        session.invalidate();
    }


    /**
     * 获取当前用户信息
     *
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result info(HttpSession session) {
        Employee employee = (Employee) session.getAttribute(Employee.SESSION_USER);
        return new Result(ResultState.SUCCESS, "登录成功", employee);
    }
}
